<template>
  <div class="app-container">
    <div style="margin-bottom: 20px;">
      <FilenameOption v-model="filename" />
      <AutoWidthOption v-model="autoWidth" />
      <BookTypeOption v-model="bookType" />
      <el-button :loading="downloadLoading" style="margin-left: 20px;" type="primary" :icon="Document" @click="handleDownload">
        Export Excel
      </el-button>
      <a href="https://panjiachen.github.io/vue-element-admin-site/feature/component/excel.html" target="_blank" style="margin-left:15px;">
        <el-tag type="info">Documentation</el-tag>
      </a>
    </div>

    <el-table v-loading="listLoading" :data="list" element-loading-text="loading..." border fit highlight-current-row>
      <el-table-column align="center" label="Id" width="95">
        <template #default="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="Title">
        <template #default="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="Author" width="110" align="center">
        <template #default="scope">
          <el-tag>{{ scope.row.author }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="Readings" width="115" align="center">
        <template #default="scope">
          {{ scope.row.pageviews }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Date" width="220">
        <template #default="scope">
          <i class="el-icon-time" />
          <span>{{ parseTime(scope.row.timestamp, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { fetchList } from '@/api/article'
import { parseTime } from '@/utils'
import { Document } from '@element-plus/icons-vue'

import FilenameOption from './components/FilenameOption'
import AutoWidthOption from './components/AutoWidthOption'
import BookTypeOption from './components/BookTypeOption'
import { export_json_to_excel } from "@/vendor/Export2Excel";

const list = ref(null)
const listLoading = ref(true)
const downloadLoading = ref(false)
const filename = ref('')
const autoWidth = ref(true)
const bookType = ref('xlsx')

const fetchData = () => {
  listLoading.value = true
  fetchList().then(response => {
    list.value = response.data.items
    listLoading.value = false
  })
}

const handleDownload = () => {
  downloadLoading.value = true
  import('@/vendor/Export2Excel').then(excel => {
    const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
    const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
    const data = formatJson(filterVal, list.value)
    excel.export_json_to_excel({
      header: tHeader,
      data,
      filename: filename.value,
      autoWidth: autoWidth.value,
      bookType: bookType.value
    })
    downloadLoading.value = false
  })
}

const formatJson = (filterVal, jsonData) => {
  return jsonData.map(v => filterVal.map(j => {
    if (j === 'timestamp') {
      return parseTime(v[j])
    } else {
      return v[j]
    }
  }))
}

fetchData()
</script>

<script>
export default {
  name: 'ExportExcel'
}
</script>

<style>
.radio-label {
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 30px;
}
</style>
